﻿Public Class baseEntryForm
    Dim newItemAdded As Boolean
    Dim mprocmaster As String
    Dim mCONTROL_CONTAINER As Control()
    Property PROCEDURE_MASTER() As String 'copy from pagelistmaster
        Get
            Return mprocmaster
        End Get
        Set(ByVal value As String)
            mprocmaster = value
        End Set
    End Property
    Property CONTROL_CONTAINER() As Control()
        Get
            Return mCONTROL_CONTAINER
        End Get
        Set(ByVal value As Control())
            mCONTROL_CONTAINER = value
        End Set
    End Property
    Dim idkodelama As String
    Public Enum FormModeEnum
        ADD
        EDIT
    End Enum
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()



    End Sub
    Dim mpagelist As baseForm
    Property PageList() As baseForm
        Get
            Return mpagelist
        End Get
        Set(ByVal value As baseForm)
            mpagelist = value
        End Set
    End Property

    Dim mformmode As FormModeEnum
    Property FormMode() As FormModeEnum
        Get
            Return mformmode
        End Get
        Set(ByVal value As FormModeEnum)
            mformmode = value
        End Set
    End Property
    Dim msinglerow As Object()
    Property SELECT_SINGLE_ROW_PARAMETER() As Object()
        Get
            Return msinglerow
        End Get
        Set(ByVal value As Object())
            msinglerow = value
        End Set
    End Property
    Dim minsert As Object()
    Property INSERT_PARAMETER() As Object()
        Get
            Return minsert
        End Get
        Set(ByVal value As Object())
            minsert = value
        End Set
    End Property
    Dim mupdate As Object()
    Property UPDATE_PARAMETER() As Object()
        Get
            Return mupdate
        End Get
        Set(ByVal value As Object())
            mupdate = value
        End Set
    End Property
    Dim mresult As DialogResult
    Public Property result() As DialogResult
        Get
            Return mresult
        End Get
        Set(ByVal value As DialogResult)
            mresult = value
        End Set
    End Property

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm As Form = Parent.Parent
        frm.Close()
    End Sub

    Overridable Sub prepareForAddition()
        enableNewValue()
        prepareFirstFocus()
    End Sub
    Private Sub cleargrid(ByVal dgv As DataGridView)
        If dgv.RowCount > 1 Then
            For i As Integer = 0 To dgv.RowCount - 2
                dgv.Rows.RemoveAt(i)
            Next
            For Each col As DataGridViewColumn In dgv.Columns
                If Not col.Name = "cari" Then
                    dgv.Rows(0).Cells(col.Index).Value = Nothing
                End If
            Next
        ElseIf dgv.RowCount = 1 Then
            For Each col As DataGridViewColumn In dgv.Columns
                If Not col.Name = "cari" Then
                    dgv.Rows(0).Cells(col.Index).Value = Nothing
                End If
            Next
        End If


    End Sub
    Overridable Sub prepareForGrid()
        If CONTROL_CONTAINER Is Nothing Then Return
        For Each Container As Control In CONTROL_CONTAINER
            For Each c As Control In Container.Controls
                If TypeOf c Is DataGridView Then
                    cleargrid(c)
                End If
            Next
        Next
    End Sub


    Private Sub PageEntryTemplate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If ImageTitle IsNot Nothing Then picTitle.Image = ImageTitle
        lblTitle.Text = Title
    End Sub

    Private Sub btnSaveAndClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If FormMode = FormModeEnum.ADD Then
            doInsertNewData()
        Else
            doUpdateData()
        End If
        result = DialogResult.OK
        'Me.ParentForm.Hide()
    End Sub

    Overridable Sub doInsertNewData()

        'Dim retval As Integer = MessageBox.Show("Apakah anda yakin akan menyimpan data ini ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        'If retval = DialogResult.Yes Then
        If Title = "Entry Rekam Medis" Then
            prepareInsertParameter()
            Exit Sub
        End If

        prepareInsertParameter()
        If Utils.exec_SP(PROCEDURE_MASTER, INSERT_PARAMETER) Then
            'MessageBox.Show("Data berhasil ditambahkan.")
            'If INSERT_PARAMETER.Length > 2 Then
            '    Utils.exec_SP("proc_zloguser", New Object() {"add", PROCEDURE_MASTER & "|add", INSERT_PARAMETER(2), Session.username})
            'Else
            '    Utils.exec_SP("proc_zloguser", New Object() {"add", PROCEDURE_MASTER & "|add", INSERT_PARAMETER(1), Session.username})
            'End If

        Else
            MessageBox.Show("Data gagal ditambahkan.")
        End If
        'End If
    End Sub




    Overridable Sub prepareFirstFocus()

    End Sub

    Overridable Sub prepareForEdit(ByVal kode As String)
        idkodelama = kode
        Dim dt As New DataTable
        prepareSelectSingleRowParameter(kode)

        If Utils.executeSP(PROCEDURE_MASTER, SELECT_SINGLE_ROW_PARAMETER, dt) Then
            enableNewValue()
            fillEditValue(dt)
        End If
    End Sub

    Protected Sub enableNewValue()
        If CONTROL_CONTAINER Is Nothing Then Return
        For Each Container As Control In CONTROL_CONTAINER
            For Each c As Control In Container.Controls
                c.Enabled = True
                If TypeOf c Is TextBox Then
                    c.Text = ""
                ElseIf TypeOf c Is ComboBox Then
                    Dim cmb As ComboBox = c
                    If cmb.Items.Count > 0 Then cmb.SelectedIndex = 0 '                    cmb.Text = ""
                ElseIf TypeOf c Is DateTimePicker Then
                    Dim dt As DateTimePicker = c
                    'kasus harus check dulu sebelum milh date
                    If dt.ShowCheckBox Then
                        dt.Checked = False
                        dt.CustomFormat = " "
                    Else
                        dt.Value = Now
                    End If

                End If
            Next
        Next
    End Sub
    Protected Sub fillEditValue(ByVal dt As DataTable)
        For Each Container As Control In CONTROL_CONTAINER
            For Each c As Control In Container.Controls
                If c.Tag IsNot Nothing AndAlso c.Tag <> "" Then
                    If Not IsDBNull(dt.Rows(0).Item(c.Tag)) Then
                        'If (TypeOf (c) Is TextBox Or TypeOf (c) Is ComboBox Or TypeOf (c) Is DateTimePicker) Then
                        If TypeOf (c) Is DateTimePicker Then
                            Dim tggl As DateTimePicker = c
                            Try
                                tggl.Value = dt.Rows(0).Item(c.Tag).ToString
                                If tggl.ShowCheckBox Then tggl.Checked = True
                            Catch ex As InvalidCastException
                                tggl.CustomFormat = " "
                            End Try
                        ElseIf TypeOf (c) Is ComboBox Then
                            Dim cbo As ComboBox = c
                            'cbo.Text = dt.Rows(0).Item(c.Tag).ToString
                            If cbo.DataSource IsNot Nothing Then
                                cbo.SelectedValue = IIf(IsDBNull(dt.Rows(0).Item(c.Tag)) Or dt.Rows(0).Item(c.Tag) = "", "1", dt.Rows(0).Item(c.Tag))
                            ElseIf cbo.Items.Count > 0 AndAlso TypeOf (cbo.Items(0)) Is ValueDescriptionPair Then

                                For Each o As ValueDescriptionPair In cbo.Items
                                    If o.Value.ToString.Equals(dt.Rows(0).Item(c.Tag).ToString) Then cbo.SelectedItem = o
                                Next
                            Else
                                cbo.Text = dt.Rows(0).Item(c.Tag).ToString
                            End If
                        ElseIf TypeOf (c) Is TextBox Then
                            c.Text = dt.Rows(0).Item(c.Tag)
                        End If
                        'End If
                    End If
                End If
            Next
        Next
    End Sub

    Overridable Sub doUpdateData()
        'TODO : mungkin ttg konfirmasi ini bs dibuat jd option yg bs ditoggle
        prepareUpdateParameter(idkodelama)
        'Dim retval As Integer = MessageBox.Show("Apakah anda yakin akan menyimpan data ini ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        'If retval = DialogResult.Yes Then
        If Utils.exec_SP(PROCEDURE_MASTER, UPDATE_PARAMETER) Then
            'MessageBox.Show("Data berhasil diupdate.")
            'Utils.exec_SP("proc_zloguser", New Object() {"add", PROCEDURE_MASTER & "|edit", UPDATE_PARAMETER(2), Session.username})
        Else
            MessageBox.Show("Data gagal diupdate.")
        End If
        'End If
    End Sub

    Private Sub btnCancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If Not newItemAdded Then
            ParentForm.DialogResult = DialogResult.Cancel
            result = DialogResult.Cancel
        Else
            ParentForm.DialogResult = DialogResult.OK
            result = DialogResult.OK
        End If
        newItemAdded = False
        ParentForm.Hide()
    End Sub

    Overridable Sub prepareSelectSingleRowParameter(ByVal kode As String)

    End Sub

    Overridable Sub prepareInsertParameter()

    End Sub



    Overridable Sub prepareUpdateParameter(ByVal idkodelama As String)

    End Sub
End Class
